package com.jiamingZhao.sessoin.impl;

import cn.hutool.core.collection.CollUtil;
import com.jiamingZhao.config.DataSourceConfig;
import com.jiamingZhao.sessoin.SqlSession;
import com.jiamingZhao.wrapper.Wrapper;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * SQL会话实现
 */
@Slf4j
public class DefaultSqlSession implements SqlSession {
    private DefaultSqlSession() {

    }

    public static DefaultSqlSession getDefaultSqlSession() {
        return new DefaultSqlSession();
    }

    @Override
    public <T> T selectOne(Wrapper<T> wrapper) {
        List<T> list = selectList(wrapper);
        if (CollUtil.isNotEmpty(list) && list.size() > 1) {
            throw new RuntimeException("查询结果数量大于1");
        }
        return list == null ? null : list.size() == 1 ? list.get(0) : null;
    }

    @Override
    public <T> List<T> selectList(Wrapper<T> wrapper) {
        return wrapper.selectList();
    }




}
